﻿using System;
using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.Comment;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.Third;
using Microsoft.EntityFrameworkCore;

namespace FoodRaidersWebAPI.Service.Second
{
    public class ArticleService
    {
        private readonly FoodContext context;
        private readonly ArticleCommentService articleCommentService;
        private readonly PictureService pictureService;

        public ArticleService(FoodContext context)
        {
            this.context = context;
            articleCommentService = new ArticleCommentService(context);
            pictureService = new PictureService(context);
        }
        //删除 
        public Article delete(int id)
        {
            var article = findByID(id);
            context.Articles.Remove(article);
            context.SaveChanges();
            return article;
        }
        //id查询 加载 简单类型
        public Article findByID(int id)
        {
            var article = context.Articles.FirstOrDefault(a => a.ArticleID == id);
            return article;
        }
        //id查询 加载 简单类型+Picture
        public Article findByIDNoCommentLoad(int id)
        {
            var article = findByID(id);
            context.Entry(article).Collection(a=>a.Pictures).Load();
            return article;
        }
        //id查询 加载 简单类型+Picture+ArticleComment(+User)
        public Article findByIDFullLoad(int id)
        {
            var article = findByID(id);
            context.Entry(article).Collection(a=>a.Pictures).Load();
            ArticleCommentLoad(article);
            return article;
        }

        //加载评论 评论内部加载 简单类型+User
        public void ArticleCommentLoad(Article article)
        {
            var list = new List<ArticleComment>();
            articleCommentService.findAllID(article.ArticleID).ForEach(i =>
            {
                list.Add(articleCommentService.findByIDFullLoad(i));
            });
            article.ArticleComments = list;
        }
        
        //ids收藏数+1
        public void addFavorite(int id)
        {
            var article = context.Articles.FirstOrDefault(a => a.ArticleID == id);
            context.Entry(article).State = EntityState.Modified;
            article.FavoriteNumber++;
            context.SaveChanges();
        }
        //id收藏数-1
        public void subFavorite(int id)
        {
            var article = context.Articles.FirstOrDefault(a => a.ArticleID == id);
            context.Entry(article).State = EntityState.Modified;
            article.FavoriteNumber--;
            context.SaveChanges();
        }
        //ids点赞数+1
        public void addPraise(int id)
        {
            var article = context.Articles.FirstOrDefault(a => a.ArticleID == id);
            context.Entry(article).State = EntityState.Modified;
            article.PraiseNumber++;
            context.SaveChanges();
        }
        //id点赞数-1
        public void subPraise(int id)
        {
            var article = context.Articles.FirstOrDefault(a => a.ArticleID == id);
            context.Entry(article).State = EntityState.Modified;
            article.PraiseNumber--;
            context.SaveChanges();
        }
        //添加评论
        public void addComment(int aid, int uid, string content)
        {
            articleCommentService.addComment(aid,uid,content);
        }
        
    }
}